package com.drama.api.company.dramas.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.drama.api.base.*;
import com.drama.api.company.dramas.domain.po.CompanyDramasPO;
import com.drama.api.company.dramas.domain.vo.*;
import com.drama.api.company.dramas.service.ICompanyDramasService;
import com.drama.api.company.video.domain.vo.CompanyDramaVideoSearchVO;
import com.drama.api.company.video.domain.vo.CompanyDramaVideoVO;
import com.drama.api.company.video.domain.vo.CompanyVideoSearchVO;
import com.drama.api.company.video.domain.vo.CompanyVideoVO;
import com.drama.api.company.video.service.ICompanyVideoService;
import com.drama.api.sys.message.service.ISysMessageService;
import com.drama.util.BindingResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@Api( tags = ApiIndex.CompanyDramasController)
@RequestMapping(value = "/api/company/dramas")
@RestController
@SaCheckLogin
public class CompanyDramasController {

    @Autowired
    private ICompanyDramasService service;
    @Autowired
    private ISysMessageService messageService;



    @ApiOperation(value = "查询商户剧目列表")
    @GetMapping(value = "/list")
    public R<RestPage<List<CompanyDramasVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute CompanyDramasSearchVO searchVO) {
        List<CompanyDramasVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage(new PageInfo(list), list);
        return new R(restPage);
    }


    @ApiOperation(value = "查询商户剧目状态数量统计")
    @GetMapping(value = "/count")
    public R<List<CompanyDramasCountVO>> count(@ApiParam(value = "查询参数") @ModelAttribute CompanyDramasCountSearchVO searchVO) {
        List<CompanyDramasCountVO> list = service.count(searchVO);
        return new R(list);
    }

    @ApiOperation(value = "商户提交剧目")
    @PostMapping(value = "/add")
    public R create(@ApiParam(value = "参数") @Valid @RequestBody CompanyDramasAddVO vo, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            service.add(vo);
        }
        return result;
    }


    @ApiOperation(value = "检查名字是否重复【第一次】")
    @GetMapping(value = "/check_name")
    public R check_name(
            @ApiParam(value = "视频名称") @RequestParam String name) {
        service.checkName(name);
        return new R();
    }

    @ApiOperation(value = "商户修改剧目【重新提交剧目】")
    @PostMapping(value = "/update")
    public R update(@ApiParam(value = "参数") @Valid @RequestBody CompanyDramasUpdateVO vo, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            service.update(vo);
        }
        return result;
    }

    @ApiOperation(value = "申请上线")
    @PostMapping(value = "/go_live")
    public R go_live(@ApiParam(value = "参数") @Valid @RequestBody CompanyDramasLiveVO vo, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            service.go_live(vo);
        }
        return new R();
    }

    @ApiOperation(value = "撤回申请")
    @PostMapping(value = "/revoke")
    public R revoke(@ApiParam(value = "参数") @Valid @RequestBody IDEntity vo, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            service.revoke(vo.getId());
        }
        return new R();
    }

    @ApiOperation(value = "申请上线重新价格【测试用】")
    @PostMapping(value = "/go_live_price")
    public R go_live_price(@ApiParam(value = "参数") @Valid @RequestBody CompanyDramasLiveVO vo, BindingResult bindingResult) {
        service.go_live_price(vo);
        return new R();
    }


    @ApiOperation(value = "查询剧目审核不通过原因")
    @GetMapping(value = "/failed_desc")
    public R<CompanyDramasScheduleVO> failed_desc(@ApiParam(value = "查询参数") @ModelAttribute IDEntity id) {
        CompanyDramasScheduleVO companyDramasScheduleVO = service.failed_desc(id.getId());
        return new R(companyDramasScheduleVO);
    }


    @ApiOperation(value = "查询剧目上线驳回原因")
    @GetMapping(value = "/reject_desc")
    public R<CompanyDramasScheduleSVO> reject_desc(@ApiParam(value = "查询参数") @ModelAttribute IDEntity id) {
        String desc = service.reject_desc(id.getId());
        CompanyDramasScheduleSVO companyDramasScheduleSVO=new CompanyDramasScheduleSVO();
        companyDramasScheduleSVO.setExamineDesc(desc);
        return new R(companyDramasScheduleSVO);
    }


    @Autowired
    private ICompanyVideoService videoService;

    @ApiOperation(value = "查询视频列表[不分页]")
    @GetMapping(value = "/video_all_list")
    public R<List<CompanyDramaVideoVO>> video_all_list(
            @ApiParam(value = "查询参数") @ModelAttribute CompanyDramaVideoSearchVO searchVO) {
        List<CompanyDramaVideoVO> list = videoService.geVideoList(searchVO);
        if(list==null){
            list=new ArrayList<>();
        }
        return new R(list);
    }


    @ApiOperation(value = "查询视频列表[分页]")
    @GetMapping(value = "/video_list")
    public R<RestPage<List<CompanyVideoVO>>> video_list(
            @ApiParam(value = "查询参数") @ModelAttribute CompanyVideoSearchVO searchVO) {
        List<CompanyVideoVO> list ;
        if(StringUtils.isNotBlank(searchVO.getDramaId())){
            CompanyDramasPO companyDramasPO=service.getDramas(searchVO.getDramaId());
            if(companyDramasPO.getDramaState()==2||companyDramasPO.getDramaState()==9){
                list = videoService.getVideosLogList(searchVO);
            }else{
                list = videoService.getVideosList(searchVO);
            }
        }else{
            list = videoService.getVideosList(searchVO);
        }
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }


    @ApiOperation(value = "查询视频日志列表【驳回使用】[分页]")
    @GetMapping(value = "/video_list_log")
    public R<RestPage<List<CompanyVideoVO>>> getVideosLogList(
            @ApiParam(value = "查询参数") @ModelAttribute CompanyVideoSearchVO searchVO) {
        List<CompanyVideoVO> list = videoService.getVideosLogList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }


    @ApiOperation(value = "删除剧目")
    @PostMapping(value = "/delete")
    public R delete(@ApiParam(value = "参数") @Valid @RequestBody IDEntity idEntity, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            service.delete(idEntity.getId());
        }
        return result;
    }


    @ApiOperation(value = "删除剧目-批量")
    @PostMapping(value = "/delete_batch")
    public R delete(@ApiParam(value = "参数") @Valid @RequestBody IDsEntity iDsEntity, BindingResult bindingResult) {
        R result = BindingResultUtil.BindingResult(bindingResult);
        if (result.getCode() == Constants.SYS_SUCCESS) {
            service.deleteBatch(iDsEntity);
        }
        return result;
    }
}